﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using blackhouse.BlackApart.Data;
using System.Globalization;
using System.Drawing;

namespace blackhouse.BlackApart.Business.Apartments {
	public class DaApartmentPhoto : IApartmentPhoto {

		#region Fields

		private IApartmentDataAccess dataAccess;
		private ImpApartmentPhoto instance;

		#endregion

		#region Properties

		public int PhotoId {
			get { return this.instance.PhotoId; }
			private set { this.instance.PhotoId = value; }
		}

		public string PhotoName {
			get { return this.instance.PhotoName; }
			set { this.instance.PhotoName = value; }
		}

		public string Category {
			get { return this.instance.Category; }
			set { this.instance.Category = value; }
		}

		public string Description {
			get { return this.instance.Description; }
			set { this.instance.Description = value; }
		}

		public int Sort {
			get { return this.instance.Sort; }
			set { this.instance.Sort = value; }
		}

		#endregion

		#region Constructors

		public DaApartmentPhoto(IApartmentDataAccess dataAccess) {
			this.dataAccess = dataAccess;
			this.instance = new ImpApartmentPhoto();
		}

		private DaApartmentPhoto(IApartmentDataAccess dataAccess, ApartmentPhotoData data) {
			this.dataAccess = dataAccess;
			this.instance = this.Convert(data);
		}

		#endregion

		#region Methods

		public void Read(ApartmentPhotoData apartmentPhotoData) {
			this.instance = this.Convert(apartmentPhotoData);
		}

		public IApartmentPhoto[] GetApartmentPhotos(int apartmentId) {
			IApartmentPhoto[] photos = this.instance.GetApartmentPhotos(apartmentId);
			if (photos == null || photos.Length == 0) {
				ApartmentPhotoData[] data = this.dataAccess.GetApartmentPhotos(apartmentId);
				photos = Array.ConvertAll(data, d => new DaApartmentPhoto(this.dataAccess, d));
				this.instance.SetApartmentPhotos(apartmentId, photos);
			}
			return photos;
		}

		public IApartmentPhoto[] GetApartmentTypePhotos(int apartmentTypeId) {
			IApartmentPhoto[] photos = this.instance.GetApartmentTypePhotos(apartmentTypeId);
			if (photos == null || photos.Length == 0) {
				ApartmentPhotoData[] data = this.dataAccess.GetApartmentTypePhotos(apartmentTypeId, CultureInfo.CurrentCulture.Name);
				photos = Array.ConvertAll(data, d => new DaApartmentPhoto(this.dataAccess, d));
				this.instance.SetApartmentTypePhotos(apartmentTypeId, photos);
			}
			return photos;
		}

		private ImpApartmentPhoto Convert(ApartmentPhotoData data) {
			ImpApartmentPhoto photo = new ImpApartmentPhoto();
			SetImp(photo, data);
			return photo;
		}

		internal static void SetImp(ImpApartmentPhoto photo, ApartmentPhotoData data) {
			photo.Category = data.Category;
			photo.Description = data.Description;
			photo.PhotoId = data.PhotoId;
			photo.PhotoName = data.PhotoName;
			photo.Sort = data.Sort;
		}

		#endregion

        public string GetImageUrl(Size size) {
            throw new NotImplementedException();
        }

    }
}
